Projectile with Air Resistance

  • PROGRAM: Projectile with air resistance
  • CREATED: 4/9/2018

In [1]:
#Import packages.
import numpy as np
import matplotlib.pylab as plt
%matplotlib notebook

In [2]:
#Set folder to save images in.
import os
#os.chdir('Folder/Address/In/Here')

In [3]:
#Rename the function 'figure' from the 'plt' library (a.k.a. the 'matplotlib.pylab' library) to make it more convenient to use.
fig = plt.figure()

#Set up axes.
ax = fig.add_subplot(1, 1, 1)



In [1]:
#Define the parameters in the problem. This way, they are located in one place in the code and can be easily changed to test different values.
g = 9.8
v_0 = 2
k = 1

h = 1/(2*k) * np.log(k*(v_0**2)/g + 1)



#Plot the function on the way up.
#Pretend to plot the function as a smooth curve by plotting a thousand points close together.
y = np.linspace(0, h, 1000)
#Above creates 1000 points at evenly-spaced locations between 0 and h. The function 'linspace' is in the package 'numpy'.

#Define the function.
v = np.sqrt( ( np.exp(-2*k*y) * (g + k*(v_0**2)) - g ) / k )

#Plot the function.
ax.plot(y, v, 'b-', label = 'On the Way Up')


#Plot the function on the way down.
#Pretend to plot the function as a smooth curve by plotting a thousand points close together.
y = np.linspace(0, h, 1000)
#Above creates 1000 points at evenly-spaced locations between 0 and h. The function 'linspace' is in the package 'numpy'.

#Define the function.
v = - np.sqrt( g * (1 - np.exp(2*k*(y-h)) ) / k )

#Plot the function.
ax.plot(y, v, 'b-', label = 'On the Way Down')



#Label the plot.
fig.suptitle('Projectile with Air Resistance in Phase Space')
ax.set_xlabel('Height, y (meters)')
ax.set_ylabel('Velocity, v (meters/s)')

ax.legend(loc = 'upper right', fancybox = False, shadow = False)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-b4fc20932b1f> in <module>()
      4 k = 1
      5 
----> 6 h = 1/(2*k) * np.log(k*(v_0**2)/g + 1)
      7 
      8 

NameError: name 'np' is not defined

In [ ]:
#Save figure by inserting a filename below.
#plt.savefig()

Notes

  • Why do we rename plt.figure()?
  • The arguments a, b, and c in fig.add_subplot(a, b, c) may be used to add multiple plots to one figure. The a says how many rows of plots the single figure has, b says how many columns it has, and c says which plot this is in the grid (filling in the grid by rows). (A picture to explain would be helpful here).